package com.tanhua.dubbo.server.controller;

import cn.hutool.core.util.ObjectUtil;
import com.tanhua.common.exception.BusinessException;
import com.tanhua.common.vo.ErrorResult;
import com.tanhua.dubbo.server.service.SoundTransmissionService;
import com.tanhua.dubbo.server.vo.SoundVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

//桃花传音
@RestController
@RequestMapping("/peachblossom")
public class SoundTransmissionController {

    @Autowired
    private SoundTransmissionService soundTransmissionService;

    //发送语音
    @PostMapping
    public ResponseEntity sendSound(@RequestParam("soundFile") MultipartFile soundFile) {


        Boolean result = this.soundTransmissionService.sendSound(soundFile);
        if (result) {
            return ResponseEntity.ok(result);
        }
        //抛出音频上传失败的
        throw new BusinessException(ErrorResult.sendSound());
    }

    //接收语音
    @GetMapping
    public ResponseEntity acceptSound() {


        SoundVo soundVo = this.soundTransmissionService.acceptSound();

        if (ObjectUtil.isNotEmpty(soundVo)) {
            return ResponseEntity.ok().body(soundVo);
        }
        throw  new BusinessException(ErrorResult.acceptSound());
    }


}
